<?php

namespace frontendm\models;

/**
 * @author  shuang
 * @date    2016-11-3 14:55:12
 * @version V1.0
 * @desc    
 */
use common\models\Task;
use yii\db\Query;
use Yii;
use yii\helpers\ArrayHelper;

class WB_Task extends Task {
    /*
     * 查询任务
     * @params $type
     */

    public static function getTaskList($type) {
        //隐藏任务ID
        $nottask = \frontendm\models\WB_TaskHidden::getHiddenTask(Yii::$app->user->id);
        //置顶任务
        if (Yii::$app->request->get("page") > 1) {
            $topinfo = ["topdata" => [], "topids" => []];
        } else {
            $topinfo = self::getTopTask();
        }
        //已做过任务ID
        $doTaskID = self::doTask();
        $notIDs = ArrayHelper::merge(ArrayHelper::merge($topinfo["topids"], $nottask ? $nottask : []), ArrayHelper::map($doTaskID, "taskid", "taskid"));
        $query = new Query();
        $query->from(WB_Task::tableName() . " as a");
        $query->leftJoin(WB_TaskPrice::tableName() . " as b", "a.task_price_id=b.id");
        $query->leftJoin("(select count(id) taskcount,taskid from " . WB_UserTaskOperate::tableName() . " group by taskid) as c", "a.id=c.taskid");
        $type ? $query->where("type=:type", [":type" => $type]) : null;
        $query->select("a.id,a.title,a.number,a.single_meet,a.is_speed,b.brush_price,c.taskcount");
        $notIDs ? $query->andWhere("a.id not in (" . implode(",", $notIDs) . ")") : null;
        $query->andWhere("status=67");
        $query->andWhere("a.number > 0");
        $query->andWhere("a.number - c.taskcount >0 or c.taskcount is null ");
        $query->orderBy("reflash_at desc");
        $countQuery = clone $query;
        $defaultPageSize = 20;
        $pager = new \yii\data\Pagination(['totalCount' => $countQuery->count(), 'defaultPageSize' => $defaultPageSize]);
        $offset = (Yii::$app->request->get("page") - 1) * $defaultPageSize;
        $limit = $defaultPageSize;
        $res = $query->offset($offset)->limit($limit)->all();
        return ["pager" => $pager, "data" => $res, "topdata" => $topinfo["topdata"]];
    }

    /*
     * 获取置顶任务
     */

    public static function getTopTask() {
        $temp = ["topdata" => [], "topids" => []];
        //查找置顶任务ID
        $result = \frontendm\models\WB_TaskTop::find()->select("taskid")->asArray()->all();
        if ($result) {
            $topid = ArrayHelper::map($result, "taskid", "taskid");
            $query = new Query();
            $query->from(WB_Task::tableName() . " as a");
            $query->leftJoin(WB_TaskPrice::tableName() . " as b", "a.task_price_id=b.id");
            $query->leftJoin("(select count(id) taskcount,taskid from " . WB_UserTaskOperate::tableName() . " group by taskid) as c", "a.id=c.taskid");
            $query->select("a.id,a.title,a.number,a.single_meet,b.brush_price,c.taskcount");
            $query->andWhere("a.id in (" . implode(",", $topid) . ")");
            $query->andWhere("status=67");
            $query->orderBy("reflash_at desc");
            $temp = ["topdata" => $query->all(), "topids" => $topid];
        }
        return $temp;
    }

    /*
     * 已做过的任务
     */

    public static function doTask() {
        //已做过的任务id
        $query = new Query();
        $query->from(WB_UserTaskOperate::tableName() . " as a");
        $query->leftJoin(WB_Task::tableName() . " as b", "b.id=a.taskid");
        $query->select("a.taskid");
        $query->andWhere("b.status=67");
        $query->andWhere("a.userid=" . Yii::$app->user->id);
        $query->andWhere("b.single_meet=1");
        $query->groupBy("taskid");
        return $query->all();
    }

}
